// pages/first/first.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav_list:[
      {id:"nav_item0",title:"推荐"},
      {id:"nav_item1",title:"女装"},
      {id:"nav_item2",title:"百货"},
      {id:"nav_item3",title:"鞋包"},
      {id:"nav_item4",title:"男装"},
      {id:"nav_item5",title:"食品"},
      {id:"nav_item6",title:"内衣"},
      {id:"nav_item7",title:"母婴"},
      {id:"nav_item8",title:"水果"},
      {id:"nav_item9",title:"运动"},
      {id:"nav_item10",title:"家装"},
      {id:"nav_item11",title:"电器"},
    ],
    current_nav:"nav_item0",
    current_template:"nav_item0",
    template_data:[]
  },
  scroll(e){
    
    this.setData({
      current_nav:e.currentTarget.id
    });
    var current = this.data.current_nav;
    this.data.current_template = current == 'nav_item0' ?  'nav_item0' : 'nav_item1';
    this.setData({
      current_template:this.data.current_template
    });
    var that = this;
    const db = wx.cloud.database();
    db.collection("first_template_data").where({
      index:current
    }).get({
      success:function(res){
        that.setData({
          template_data:res.data[0]
        });  
        console.log(template_data)
      }
    });
  },
  login(e){
    const db = wx.cloud.database();
    db.collection('userinfo').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        uname:"张三",
        pwd:"1234567"
      },
      success: function(res) {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log(res)
      }
    });
  },
  check(){
    const db = wx.cloud.database();
    db.collection("userinfo").where({
      uname:"张三"
    }).get({
      success:function(res){
        console.log(res.data[0]._id);
      }
    });
  },
  cloudHandle(){
    wx.cloud.callFunction({
      // 云函数名称
      name: 'add',
      // 传给云函数的参数
      data: {
        a: 1,
        b: 2,
      },
      success: function(res) {
        console.log(res) // 3
      },
      fail: console.error
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var current = this.data.current_nav;
    var that = this;
    const db = wx.cloud.database();
    db.collection("first_template_data").where({
      index:current
    }).get({
      success:function(res){
        that.setData({
          template_data:res.data[0]
        });  
        console.log(template_data);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})